package myjava.binding;

public class Root1 {

   public static void main(String[] args) {

      // reference : http://geekexplains.blogspot.com/2008/06/dynamic-binding-vs-static-binding-in.html
      A aa = new A();
      A ab = new B();
      B bb = new B();

      // Static Binding AKA Early Binding
      // static method calls are always resolved at compile time (Early binding)
      // [!] l'accesso via istanza di un metodo statico e' sconsigliato perche' fuorviante!
      aa.staticMeth();   // static in A
      ab.staticMeth();   // static in A
      bb.staticMeth();   // static in B

      // Also member variables are resolved at compile time!
      System.out.println(aa.name); // name in A
      System.out.println(ab.name); // name in A
      System.out.println(bb.name); // name in B

      // Dynamic Binding AKA Late Binding
      // instance method calls are always resolved at runtime (Late binding)
      aa.foo();    // foo in A
      ab.foo();    // foo in B
      bb.foo();    // foo in B

      // and finally: static methods are inherited and accessible both through the object and the class!
      B.inheritable();
      bb.inheritable();
   }
}

class A {

   public String name = "name in A";

   public static void inheritable() {
      System.out.println("inheritable");
   }

   public static void staticMeth() {
      System.out.println("static in A");
   }

   public void foo() {
      System.out.println("foo in A");
   }
}

class B extends A {

   public String name = "name in B";

   public static void staticMeth() {
      System.out.println("static in B");
   }

   @Override
   public void foo() {
      System.out.println("foo in B");
   }
}
